package com.paiai.lslease.action.apphouse;

import com.paiai.lslease.auto.model.AppRepairs;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.service.apprepairs.AppRepairsService;
import com.paiai.lslease.service.apphouse.AppLockService;
import com.paiai.lslease.service.appuser.AppSysUserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.*;

public class AppRepairsAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(AppRepairsAction.class);

    private String userId;
    private AppRepairsService appRepairsService;
    private AppLockService appLockService;
    private String repairsId;
    private String operateUserId;
    private String tenantId;
    private String landlordId;
    private String lockId;
    private String title;
    private String content;

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setAppRepairsService(AppRepairsService appRepairsService) {
        this.appRepairsService = appRepairsService;
    }

    public void setAppLockService(AppLockService appLockService) {
        this.appLockService = appLockService;
    }

    public void setRepairsId(String repairsId) {
        this.repairsId = repairsId;
    }

    public void setOperateUserId(String operateUserId) {
        this.operateUserId = operateUserId;
    }

    public void setTenantId(String tenantId) {
        this.tenantId = tenantId;
    }

    public void setLandlordId(String landlordId) {
        this.landlordId = landlordId;
    }

    public void setLockId(String lockId) {
        this.lockId = lockId;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 租客向房东报修
     */
    public void repairsToLandlord() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.userExist(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(tenantId)) {
                sendFailureMsg("租客ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(tenantId)) {
                sendFailureMsg("租客ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongTenant(Integer.parseInt(tenantId))) {
                sendFailureMsg("租客不存在");
                return;
            }
            if (StringUtils.isEmpty(landlordId)) {
                sendFailureMsg("房东ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(landlordId)) {
                sendFailureMsg("房东ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(landlordId))) {
                sendFailureMsg("房东不存在");
                return;
            }
            if (StringUtils.isEmpty(lockId)) {
                sendFailureMsg("设备ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockId)) {
                sendFailureMsg("设备ID格式错误");
                return;
            }
            if (!appLockService.lockExistById(Integer.parseInt(lockId))) {
                sendFailureMsg("设备不存在");
                return;
            }
            if (!appLockService.lockTenantBelongLandlord(Integer.parseInt(tenantId), Integer.parseInt(landlordId))) {
                sendFailureMsg("该租客不属于该房东，不能报修");
                return;
            }
            if (!appLockService.lockBelongUser(Integer.parseInt(lockId), Integer.parseInt(landlordId))) {
                sendFailureMsg("该设备不属于该房东，不能报修");
                return;
            }
            if (StringUtils.isEmpty(title)) {
                sendFailureMsg("报修的标题不能为空");
                return;
            }
            if (StringUtils.isEmpty(content)) {
                sendFailureMsg("报修的内容不能为空");
                return;
            }
            AppRepairs appRepairsToInsert = new AppRepairs();
            appRepairsToInsert.setTenantId(Integer.valueOf(tenantId));
            appRepairsToInsert.setLandlordId(Integer.parseInt(landlordId));
            appRepairsToInsert.setLockId(Integer.parseInt(lockId));
            appRepairsToInsert.setTitle(title);
            appRepairsToInsert.setContent(content);
            appRepairsToInsert.setItem1("1");
            appRepairsToInsert.setInsertTime(new Date());
            appRepairsToInsert.setUpdateTime(new Date());
            AppRepairs appRepairs = appRepairsService.repairsToLandlord(appRepairsToInsert);
            if (appRepairs == null) {
                sendFailureMsg("报修失败");
                return;
            }
            sendSuccesseMsg(appRepairs, "报修成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("报修失败");
        }
    }

    /**
     * 房东查询租客提交的报修列表
     */
    public void selectRepairsListByLanlord() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.userExist(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(landlordId)) {
                sendFailureMsg("房东ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(landlordId)) {
                sendFailureMsg("房东ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(landlordId))) {
                sendFailureMsg("房东不存在");
                return;
            }
            List<AppRepairs> appRepairsList = appRepairsService.selectRepairsListByLanlord(Integer.parseInt(landlordId));
            Collections.sort(appRepairsList, new Comparator<AppRepairs>() {
                @Override
                public int compare(AppRepairs o1, AppRepairs o2) {
                    return (int) (o2.getUpdateTime().getTime()/1000 - o1.getUpdateTime().getTime()/1000);
                }
            });
            sendSuccesseMsg(appRepairsList, "查询租客提交的报修列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询租客提交的报修列表失败");
        }
    }

    /**
     * 租客查询自己提交的报修列表
     */
    public void selectRepairsListByTenant() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.userExist(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(tenantId)) {
                sendFailureMsg("租客ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(tenantId)) {
                sendFailureMsg("租客ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongTenant(Integer.parseInt(tenantId))) {
                sendFailureMsg("租客不存在");
                return;
            }
            List<AppRepairs> appRepairsList = appRepairsService.selectRepairsListByTenant(Integer.parseInt(tenantId));
            Collections.sort(appRepairsList, new Comparator<AppRepairs>() {
                @Override
                public int compare(AppRepairs o1, AppRepairs o2) {
                    return (int) (o2.getUpdateTime().getTime()/1000 - o1.getUpdateTime().getTime()/1000);
                }
            });
            sendSuccesseMsg(appRepairsList, "租客查询自己提交的报修列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("租客查询自己提交的报修列表失败");
        }
    }

    /**
     * 根据报修ID查询单条报修信息
     */
    public void selectSingleRepairsInfoById() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.userExist(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(repairsId)) {
                sendFailureMsg("报修的ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(repairsId)) {
                sendFailureMsg("报修的ID格式错误");
                return;
            }
            AppRepairs appRepairs = appRepairsService.selectSingleRepairsInfoById(Integer.parseInt(repairsId));
            if (appRepairs == null) {
                sendFailureMsg("未查询到报修记录");
                return;
            }
            sendSuccesseMsg(appRepairs, "根据报修ID查询单条报修信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("根据报修ID查询单条报修信息失败");
        }
    }

    /**
     * 查询用户的报修信息<br/>
     * 租客查询自己提交的报修列表<br/>
     * 房东查询租客提交的报修列表
     */
    public void selectRepairsList() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.userExist(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(userId)) {
                sendFailureMsg("用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(userId)) {
                sendFailureMsg("用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(userId))) {
                sendFailureMsg("用户未注册");
                return;
            }
            boolean belongTenant = appSysUserService.userBelongTenant(Integer.parseInt(userId));
            boolean belongLandlord = appSysUserService.userBelongLandlord(Integer.parseInt(userId));
            boolean belongAuthManager = appSysUserService.userBelongLandlordAuthManagerById(Integer.parseInt(userId));
            if (!belongTenant && !belongLandlord && !belongAuthManager) {
//            sendFailureMsg("用户既不是租客也不是房东，也不是授权的系统管理员，不能查看报修信息");
                sendSuccesseMsg(null, "用户既不是租客也不是房东，也不是授权的系统管理员，不能查看报修信息");
                return;
            }
            List<AppRepairs> appRepairsList = new ArrayList<AppRepairs>();
            if (belongTenant) {
                List<AppRepairs> appRepairsList1 = appRepairsService.selectRepairsListByTenant(Integer.parseInt(userId));
                if (appRepairsList1 != null) {
                    appRepairsList.addAll(appRepairsList1);
                }
            }
            if (belongLandlord){
                List<AppRepairs> appRepairsList2 = appRepairsService.selectRepairsListByLanlord(Integer.parseInt(userId));
                if (appRepairsList2 != null) {
                    appRepairsList.addAll(appRepairsList2);
                }
            }
            if (belongAuthManager){
                // 2017/10/25 房东授权的管理员，可以查看自己管理的房源的报修列表
                List<AppRepairs> appRepairsList3 = appRepairsService.selectRepairsListByAuthManager(Integer.parseInt(userId));
                if (appRepairsList3 != null) {
                    appRepairsList.addAll(appRepairsList3);
                }
            }
            Collections.sort(appRepairsList, new Comparator<AppRepairs>() {
                @Override
                public int compare(AppRepairs o1, AppRepairs o2) {
                    return (int) (o2.getUpdateTime().getTime()/1000 - o1.getUpdateTime().getTime()/1000);
                }
            });
            sendSuccesseMsg(appRepairsList, "查询用户的报修信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询用户的报修信息失败");
        }
    }
}
